
{***********************************************************}
{                                                           }
{      Informix esql "datetime.h"                           }
{                                                           }
{      Translated to Object Pascal by                       }
{      Copyright (c) 2001-2008 Luxena Software Company      }
{      http://www.luxena.com                                }
{      e-mail: support@luxena.com                           }
{                                                           }
{***********************************************************}

unit IfxDateTime;

{$I lx.inc}

{$IFDEF VER140_OR_ABOVE}
  {$ALIGN 8}
{$ENDIF}

interface

uses
  IfxTypes, IfxDecimal;

const

  // time units for datetime qualifier

  TU_YEAR = 0;
  TU_MONTH = 2;
  TU_DAY = 4;
  TU_HOUR = 6;
  TU_MINUTE = 8;
  TU_SECOND = 10;
  TU_FRAC = 12;
  TU_F1 = 11;
  TU_F2 = 12;
  TU_F3 = 13;
  TU_F4 = 14;
  TU_F5 = 15;


type

  dtime = record
    dt_qual: int2;
{$IFDEF OSF_MLS}
    dt_dec: TIx_dec;
{$ELSE}
    dt_dec: TDec;
{$ENDIF }
  end;

  PDTime = ^TDTime;
  TDTime = dtime;


  intrvl = record
    in_qual: int2;
{$IFDEF OSF_MLS}
    in_dec: TIx_dec;
{$ELSE}
    in_dec: TDec;
{$ENDIF}
  end;
  PIntrvl = ^TIntrvl;
  TIntrvl = intrvl;


  // TU_END - end time unit in datetime qualifier
  // TU_START - start time unit in datetime qualifier
  // TU_LEN - length in  digits of datetime qualifier

// #DEFINE TU_END(qual) (qual & 0xf)
function TU_END(qual: Integer): Integer;

// #DEFINE TU_START(qual) ((qual>>4) & 0xf)
function TU_START(qual: Integer): Integer;

// #DEFINE TU_LEN(qual) ((qual>>8) & 0xff)
function TU_LEN(qual: Integer): Integer;



// TU_ENCODE - encode length, start and end time unit to form qualifier
// TU_DTENCODE - encode datetime qual
// TU_IENCODE - encode interval qual

// #define TU_ENCODE(len,s,e) (((len)<<8) | ((s)<<4) | (e))
function TU_ENCODE(len, s, e: Integer): Integer;

// #define TU_DTENCODE(s,e) TU_ENCODE(((e)-(s)+((s)==TU_YEAR?4:2)), s, e)
function TU_DTENCODE(s, e: Integer): Integer;

// #define TU_IENCODE(len,s,e) TU_ENCODE(((e)-(s)+(len)),s,e)
function TU_IENCODE(len, s, e: Integer): Integer;

// #define TU_FLEN(len) (TU_LEN(len)-(TU_END(len)-TU_START(len)))
function TU_FLEN(len: Integer): Integer;


// TU_FIELDNO - field number of the given TU_ macro.
//   (e.g. year is 0, month is 1, day is 2, TU_F1 is  7, TU_F5 is 11)

// #define TU_FIELDNO(f)   (f > TU_SECOND ? (5+(f-TU_SECOND)) : (f/2))
function TU_FIELDNO(f: Integer): Integer;


// TU_CURRQUAL - default qualifier used by current

// #define TU_CURRQUAL TU_ENCODE(17,TU_YEAR,TU_F3)
function TU_CURRQUAL: Integer;

implementation

function TU_END(qual: Integer): Integer;
begin
  // TU_END(qual) (qual & 0xf)
  Result := qual and $F;
end;

function TU_START(qual: Integer): Integer;
begin
  // TU_START(qual) ((qual>>4) & 0xf)
  Result := (qual shr 4) and $F;
end;

function TU_LEN(qual: Integer): Integer;
begin
  // TU_LEN(qual) ((qual>>8) & 0xff)
  Result := (qual shr 8) and $FF;
end;

function TU_ENCODE(len, s, e: Integer): Integer;
begin
  // #define TU_ENCODE(len,s,e) (((len)<<8) | ((s)<<4) | (e))
  Result := (len shl 8) or (s shl 4) or e;
end;

function TU_DTENCODE(s, e: Integer): Integer;
var
  y: Integer;
begin
  // #define TU_DTENCODE(s,e) TU_ENCODE(((e)-(s)+((s)==TU_YEAR?4:2)), s, e)
  if s = TU_YEAR then y := 4 else y := 2;
  Result := TU_ENCODE(e - s + y, s, e)
end;

function TU_IENCODE(len, s, e: Integer): Integer;
begin
  // #define TU_IENCODE(len,s,e) TU_ENCODE(((e)-(s)+(len)),s,e)
  Result := TU_ENCODE(e - s + len, s, e);
end;

function TU_FLEN(len: Integer): Integer;
begin
  // #define TU_FLEN(len) (TU_LEN(len)-(TU_END(len)-TU_START(len)))
  Result := TU_LEN(len) - (TU_END(len) - TU_START(len));
end;

function TU_FIELDNO(f: Integer): Integer;
begin
  // #define TU_FIELDNO(f)   (f > TU_SECOND ? (5+(f-TU_SECOND)) : (f/2))
  if f > TU_SECOND then
    Result := 5 + (f - TU_SECOND) else
    Result := f div 2;
end;

function TU_CURRQUAL: Integer;
begin
  // #define TU_CURRQUAL TU_ENCODE(17,TU_YEAR,TU_F3)
  Result := TU_ENCODE(17,TU_YEAR,TU_F3);
end;

end.